#include "junix.h"

typedef unsigned chartype;

char *jstrstr(const char *phaystack, const char *pneedle)
{
	register const unsigned char *haystack, *needle;
	register chartype b, c;

	haystack = (const unsigned char *) phaystack;
	needle = (const unsigned char *) pneedle;

	b = *needle;
	if (b != '\0') {
		haystack--;		/* possible ANSI violation */
		do {
			c = *++haystack;
			if (c == '\0')
				goto ret0;
		}
		while (c != b);

		c = *++needle;
		if (c == '\0')
			goto foundneedle;
		++needle;
		goto jin;

		for (;;) {
			register chartype a;
			register const unsigned char *rhaystack, *rneedle;

			do {
				a = *++haystack;
				if (a == '\0')
					goto ret0;
				if (a == b)
					break;
				a = *++haystack;
				if (a == '\0')
					goto ret0;
shloop:;
			}
			while (a != b);

jin:a = *++haystack;
			if (a == '\0')
				goto ret0;

			if (a != c)
				goto shloop;

			rhaystack = haystack-- + 1;
			rneedle = needle;
			a = *rneedle;

			if (*rhaystack == a)
				do {
					if (a == '\0')
						goto foundneedle;
					++rhaystack;
					a = *++needle;
					if (*rhaystack != a)
						break;
					if (a == '\0')
						goto foundneedle;
					++rhaystack;
					a = *++needle;
				}
				while (*rhaystack == a);

			needle = rneedle;	/* took the register-poor approach */

			if (a == '\0')
				break;
		}
	}
foundneedle:
	return (char *) haystack;
ret0:
	return 0;
}

int main(int argc, char **argv)
{
	char *t = "hello,worlld!Good\n";
	char *p = "lld";
	char *q;

	q=jstrstr(t,p);
    exit(0);
}

